import { ajax } from '../lib/ajax.js';


//#region  获取登录信息
let userinfo = {};
function queryUser() {
  ajax.get('/auth', {}, (data) => {
    if(data.success) {
      userinfo = data.data.tbUserInfo;
    }else {
      alert(data.message);
      // location.href = 'login.html';
    }
  });
}
function showInfo() {
  txt_nickname.value = userinfo.nickname;
  txt_qq.value = userinfo.qq;
  txt_wechat.value = userinfo.wechat;
  txt_info.value = userinfo.info;
  if('m'==userinfo.sex) {
    rdo_m.checked = true;
  }else if('f'==userinfo.sex) {
    rdo_f.checked = true;
  }else if ('n'==userinfo.sex) {
    rdo_n.checked = true;
  }
  }
  


queryUser();

//#endregion

//#region 修改信息

let txt_nickname = document.getElementById('txt_nickname');
let txt_qq = document.getElementById('txt_qq');
let txt_wechat = document.getElementById('txt_wechat');
let txt_info = document.getElementById('txt_info');
let rdo_m = document.getElementById('rdo_m');
let rdo_f = document.getElementById('rdo_f');
let rdo_n = document.getElementById('rdo_n');
let btnSave = document.getElementById('btnSave');
let spInfo = document.getElementById('spInfo');


btnSave.addEventListener('click', () => {
  spInfo.innerHTML = '';
  let user = {
    nickname: txt_nickname.value,
    qq: txt_qq.value,
    wechat: txt_wechat.value,
    info: txt_info.value,
  };
  if (rdo_m.checked) {
    user.sex = 'm';
  } else if (rdo_f.checked) {
    user.sex = 'f';
  } else if (rdo_n.checked) {
    user.sex = 'n';
  }
  ajax.put('/user', user, (data) => {
    spInfo.innerHTML = data.message;
  });
});

//#endregion
